<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卖方姓名" prop="sellerName">
        <el-input
          v-model="queryParams.sellerName"
          placeholder="请输入卖方姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="买方姓名" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入买方姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成交时间">
        <el-date-picker
          v-model="rangeDate"
          type="daterange"
          range-separator="-"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:contract:transactionType:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="卖方姓名" align="center" prop="sellerName" />
      <el-table-column label="买方姓名" align="center" prop="buyerName" />
      <el-table-column label="房屋地址" align="center" prop="houseLocation" />
      <el-table-column label="成交日期" align="center" prop="saleDate" />
      <el-table-column label="出售总价(元)" align="center" prop="totalPrice">
        <template #default="scope">
          <span>{{
            scope.row.totalPrice ? `${scope.row.totalPrice}元` : `0元`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签约员" align="center" prop="signatoryName" />
      <el-table-column label="审核状态" align="center" prop="isProcess">
        <template #default="scope">
          <div
            style="display: flex; align-items: center; justify-content: center"
          >
            <el-tag type="info" v-if="scope.row.isProcess == 0">未审核</el-tag>
            <el-tag type="" v-if="scope.row.isProcess == 1">待审核</el-tag>

            <el-tag type="success" v-if="scope.row.isProcess == 3"
              >已通过</el-tag
            >
            <el-tooltip
              trigger="click"
              :content="scope.row.processRemark"
              placement="top"
              v-if="scope.row.isProcess == 2"
            >
              <el-tag type="error"> 已驳回 </el-tag>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.isProcess == 0 || scope.row.isProcess == 2"
            v-hasPermi="['system:contract:transactionConfirm:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:contract:transactionConfirm:query']"
            >查看</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleExport(scope.row)"
            v-hasPermi="['system:contract:transactionConfirm:export']"
            >导出</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleSubmit(scope.row)"
            v-if="scope.row.isProcess == 0 || scope.row.isProcess == 2"
            v-hasPermi="['system:contract:transactionConfirm:submitReview']"
            >提交审核</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleFinish(scope.row)"
            v-if="scope.row.isProcess == 3 && scope.row.isCompleted == 1"
            v-hasPermi="['system:finishReg:generateCompleted']"
            >办结</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情 -->
    <el-dialog title="查看详情" v-model="open" width="1200px" append-to-body>
      <TcFormDetail :key="tcFormDetail?.id" :msgData="tcFormDetail" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="cancel">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="tcForm">
import {
  addTransactionType,
  editTransactionType,
  delTransactionType,
} from "@/api/contract/transactionType";
import { listTcForm, submitReview, finishTcForm } from "@/api/business/tcForm";
const router = useRouter();

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
import { transactionConfirm } from "@/api/business/tcForm";
import TcFormDetail from "./components/TcFormDetail.vue";
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const rangeDate = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    businessName: [
      { required: true, message: "类型名称不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 办结
function handleFinish(row) {
  console.log(row);
  const { id } = row;
  proxy.$modal
    .confirm("是否确认办结？")
    .then(function () {
      return finishTcForm(id);
    })
    .then(() => {
      proxy.$modal.msgSuccess("办结成功");
      getList();
    });
}
/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listTcForm(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
}
const tcFormDetail = ref({});
// 查看详情
function handleDetail(row) {
  const { id } = row;
  transactionConfirm(id).then((res) => {
    tcFormDetail.value = res.data;
    open.value = true;
  });
}
//提交审核
function handleSubmit(row) {
  proxy.$modal
    .confirm("是否确认提交审核？")
    .then(function () {
      return submitReview({ id: row.id });
    })
    .then(() => {
      proxy.$modal.msgSuccess("提交成功");
      getList();
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  const [signStartTime, signEndTime] = rangeDate.value;
  queryParams.value.pageNum = 1;
  queryParams.value.signStartTime = signStartTime;
  queryParams.value.signEndTime = signEndTime;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  rangeDate.value = [];
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  router.push({
    path: "addTcForm",
    query: { id },
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        editTransactionType(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTransactionType(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(function () {
      return delTransactionType(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport(row) {
  proxy.download(
    "system/contract/transactionConfirm/export",
    {
      id: row.id,
    },
    `tcForm_${new Date().getTime()}.xlsx`
  );
}
// function handleExport(row) {
//   exportTcForm({ id: row.id });
// }

getList();
</script>
